import threading
import time

class Producer(threading.Thread):
    def __init__(self,name):
        threading.Thread.__init__(self,name= name)

        def run(self):
            global count
            while True:
                if con.acquire():
                    if count > 1000:
                        con.wait()
                    else :
                        count += 100
                        print "producinh ..." + str(count)
                        time.sleep(0.3)
                        con.notify()
                    con.release()
                    time.sleep(0.3)

class Consumer(threading.Thread):
    def __init__(self,t_name):
        threading.Thread.__init__(self,name =t_name)

    def run(self):
        global count
        while True:
            if con.acquire():
                if count < 100:
                    print 'cosumer wait1'
                    con.wait()
                else:
                    count = count -100
                    print "consuming ..."+str(count)
                    time.sleep(0.3)
                    con.notify()
                con.release()
                time.sleep(0.3)

count = 500
con = threading.Condition()


def test():
    print 'start ponsumer'
    for i in range(2):
        p = Producer('producer'+str(i))
        p.start()
    print 'start consumer'
    for i in range(5):
        c= Consumer('consumer'+str(i))
        c.start()

if __name__=="__main__":
    test()
    
